<?php
namespace app\admin\controller\news;

use app\admin\BaseController;
use app\admin\model\NewsCategory;
use app\lib\exception\FailedException;
use app\Validate\CategoryValidate;
use app\Validate\IdMustPositive;
use app\Validate\NewsValidate;
use app\Validate\PageMustValidate;
use think\Request;


class News extends BaseController
{
    /**
     * 获取新闻分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function newsCategory(Request $request){
        $post = $request->post();
        $category = (new NewsCategory())->selectList([],'id,name,create_time','sort desc');
        return writeJson(200,['category'=>$category]);
    }
    /**
     * 资讯新闻列表
     * @param Request $request
     * @throws \app\lib\exception\ParameterException
     */
    public function newsList(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where = [];
        $where = addWhere($post,['category_id'=>''],$where,1);
        $where = addWhere($post,['title'=>''],$where,2);
        $where['page'] = $post['page'];
        $list = (new \app\admin\model\News())->wechatListTotal($where,"*","sort desc",$post['page_number']);
        foreach ($list['data'] as $key=>$value){
            $list['data'][$key]['content'] = html_entity_decode($value['content']);
            $list['data'][$key]['content'] = html_entity_decode($list['data'][$key]['content']);
        }
        return writeJson(200,$list);
    }

    /**
     * 资讯添加
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function newsAdd(Request $request){
        (new NewsValidate())->goCheck();
        $post = $request->post();
        $new = [
            'title'=>$post['title'],
            'content'=>$post['content'],
            'category_id'=>$post['category_id'],
            'cover_img'=>$post['cover_img'],
            'sort'=>$post['sort'],
        ];
        $res = (new \app\admin\model\News())->dataUpdate($new);
        if(!$res){
            throw new FailedException(['msg'=>'资讯添加失败']);
        }
        return writeJson(200,[],'资讯添加成功');

    }
    /**
     * 资讯新闻编辑
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function newsEdit(Request $request){
        (new IdMustPositive())->goCheck();
        (new NewsValidate())->goCheck();
        $post = $request->post();
        $new = [
            'id'   =>$post['id'],
            'title'=>$post['title'],
            'content'=>$post['content'],
            'category_id'=>$post['category_id'],
            'cover_img'=>$post['cover_img'],
            'sort'=>$post['sort'],
        ];
        $res = (new \app\admin\model\News())->dataUpdate($new);
        if(!$res){
            throw new FailedException(['msg'=>'资讯编辑失败']);
        }
        return writeJson(200,[],'资讯编辑成功');
    }
    /**
     * 资讯新闻删除
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function newsDelete(Request $request){
        $post = $request->post();
        if(count($post['id'])==0){
            throw new FailedException(['msg'=>'请选择需要删除的资讯']);
        }
        $res = (new \app\admin\model\News())->fieldUpdate('delete_time',time(),"",[['id','in',$post['id']]]);
        if(!$res){
            throw new FailedException(['msg'=>'资讯删除失败']);
        }
        return writeJson(200,[],'资讯删除成功');
    }
}